#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from Window import Ui_MainWindow
from Proxy import Proxy
import signal

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def toggle_connect(self):
        self.ui.button_connect.setIcon(QIcon(":/window/connect.png"))
        self.ui.lineEdit_address.setEnabled(False)
        self.ui.lineEdit_port.setEnabled(False)

    def toggle_disconnect(self):
        self.ui.button_connect.setIcon(QIcon(":/window/disconnect.png"))
        self.ui.lineEdit_address.setEnabled(True)
        self.ui.lineEdit_port.setEnabled(True)

def sigint_handler(*args):
    QApplication.quit()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, sigint_handler)
    app = QApplication(sys.argv)
    w = MyMainWindow()
    proxy = Proxy(w.ui)
    QObject.connect(w.ui.button_connect, SIGNAL('clicked()'), proxy.toggle)
    proxy.toggle_connect_sig.connect(w.toggle_connect)
    proxy.toggle_disconnect_sig.connect(w.toggle_disconnect)
    w.show()
    code = app.exec_()
    proxy.stop()
    proxy.wait()
    sys.exit(code)

